package com.woniuxy.cq.phrase3.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.cq.phrase3.common.DateUtil;
import com.woniuxy.cq.phrase3.common.Result;
import com.woniuxy.cq.phrase3.common.ValidationUtil;
import com.woniuxy.cq.phrase3.entity.Project;
import com.woniuxy.cq.phrase3.service.ILeaderService;
import com.woniuxy.cq.phrase3.service.IUserService;
import com.woniuxy.cq.phrase3.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class LeaderController {

    @Autowired
    private ILeaderService iLeaderService;

    @Autowired
    private UserService userService;
    /**
     * 查询用户Id
     */
    @RequestMapping("/selectLeaderId")
    @RequiresRoles("decision")
    public Result SelectUserId(HttpSession session){
      //System.out.println("=======");
        Integer userId = (Integer)session.getAttribute("userId");
        String roles = userService.findRolesByUserId(100);
        return Result.success(userId);

       // return Result.success(1);
    }

    /**
     * 分发请求的
     * @param leaderId
     * @param projectId
     * @param projectStatus
     * @return
     */
    @RequestMapping("/dispacheroperation")
    @RequiresRoles("decision")
    public Result Dispacheroperation(Integer leaderId, Integer projectId,String projectStatus){
        Result  result= iLeaderService.dispacheroperation(leaderId,projectId,projectStatus);
        return result;
    }
    /**
     * 查询待处理的事项
     *
     * @param leaderId
     * @return
     */
    @RequestMapping("/leaderList")
    @RequiresRoles("decision")
    public Result leaderList(Integer leaderId) {
        log.info("领导Id{}", leaderId);
        //根据领导Id查询角色；主任和局长查看到的事项不一样
        List<Project> leaderList = iLeaderService.leaderListByChairman(leaderId);
        return Result.success(leaderList);
    }
    /**
     * 分页查询 局长
     */
    @RequestMapping("chairmanPageList")
    @RequiresRoles("decision")
    public Result chairmanPageList( @RequestParam(defaultValue = "1") int pageIdx, @RequestParam(defaultValue = "10") int pageSize, Integer  leaderId, String projectStatus, String projectName
            , ModelMap modelMap){
        //参数处理
        System.out.println(projectStatus+"=====》》》》》》"+projectName+"====<<<<<<"+leaderId    );
        IPage<Project> projectIpage = iLeaderService.chairmanPageList(pageIdx,pageSize,projectStatus,projectName);
        //modelMap.addAttribute("teacherIpage",teacherIpage);
        return Result.success(projectIpage);
    }
    /**
     * 领导同意该审批
     *
     * @param leaderId  领导Id
     * @param projectId
     * @return
     */
    @RequestMapping("/chairmanAllow")
    @RequiresRoles("decision")
    public Result ChairmanAllow(Integer leaderId, Integer projectId) {
        log.info("领导Id为{}批准交与省政府  项目{}于{}", leaderId, projectId, DateUtil.getCurrentTime());
        iLeaderService.chairmanAllow(projectId);
        return Result.success();
    }

    /**
     * 局长不批准项目
     *
     * @param leaderId
     * @param projectId
     * @return
     */
    @RequestMapping("/chairmanFail")
    @RequiresRoles("decision")
    public Result ChairmanFail(Integer leaderId, Integer projectId) {
        log.info("领导Id为{}审批失败项目{}于{}", leaderId, projectId, DateUtil.getCurrentTime());
        iLeaderService.chairmanFail(projectId);
        return Result.success();
    }

    /**
     * 局长直接批准项目
     *
     * @param leaderId
     * @param projectId
     * @return
     */
    @RequestMapping("chairmanPass")
    @RequiresRoles("decision")
    public Result ChairmanPass(Integer leaderId, Integer projectId) {
        log.info("领导Id为{}直接批准项目{}于{}", leaderId, projectId, DateUtil.getCurrentTime());
        iLeaderService.chairmanPass(projectId);

        return Result.success();
    }

    /**
     * 主任同意该审批
     *
     * @param leaderId  领导Id
     * @param projectId
     * @return
     */
    @RequestMapping("/directorAllow")
    @RequiresRoles("decision")
    public Result DirectorAllow(Integer leaderId, Integer projectId) {
        log.info("领导Id为{}批准交与省政府  项目{}于{}", leaderId, projectId, DateUtil.getCurrentTime());
        iLeaderService.directorAllow(projectId);
        return Result.success();
    }

    /**
     * 局长不批准项目
     *
     * @param leaderId
     * @param projectId
     * @return
     */
    @RequestMapping("/directorFail")
    @RequiresRoles("decision")
    public Result DirectorFail(Integer leaderId, Integer projectId) {
        log.info("领导Id为{}审批失败项目{}于{}", leaderId, projectId, DateUtil.getCurrentTime());
        iLeaderService.directorFail(projectId);
        return Result.success();
    }

    /**
     * 局长直接批准项目
     *
     * @param leaderId
     * @param projectId
     * @return
     */
    @RequestMapping("/directorPass")
    @RequiresRoles("decision")
    public Result DirectorPass(Integer leaderId, Integer projectId) {
        log.info("领导Id为{}直接批准项目{}于{}", leaderId, projectId, DateUtil.getCurrentTime());
        iLeaderService.directorPass(projectId);
        return Result.success();
    }
}
